<?php
namespace app\web\controller;

use think\Request;
use think\Db;
use think\Log;
use app\web\model\GzSms as GzSms;
use app\web\service\Reissue;
use app\web\service\MaoyanApi as my;
class Qy extends Common
{



    /**
     * 中移金科领券接口 
     * */
    public function zyjk(){

        /*
        猫眼月卡：BlBQVA-m
        */
        $param = request()->post();

        $cmicrwx =  new \app\web\service\Cmicrwx();
        if( empty($param) ){
            $body = ["resultCode"=>"0004","resultMsg"=>"参数非法"];
            $data = $cmicrwx->returnMsg($param,$body);
            return json($data);
        }

        logsToFile('zyjk.txt','入参参数:_'.json_encode($param,256));

        //判断body
        if( !isset($param['contractRoot']['body']) ){
            $body = ["resultCode"=>"0004","resultMsg"=>"body非法"];
            $data = $cmicrwx->returnMsg($param,$body);
            return json($data);
        }
        //判断手机号
        $phone = $param['contractRoot']['body']['serverNum'];
        // $is_mobile = preg_match('#^1[0-9][0-9]\d{4,8}$#', $phone) ? true : false;
        if(empty($phone)){
            $body = ["resultCode"=>"0004","resultMsg"=>"serverNum非法"];
            $data = $cmicrwx->returnMsg($param,$body);
            return json($data);
        }

        if( empty($param['contractRoot']['body']['orderItemId'] ) ){
            $body = ["resultCode"=>"0004","resultMsg"=>"orderItemId非法"];
            $data = $cmicrwx->returnMsg($param,$body);
            return json($data);
        }

        if( empty($param['contractRoot']['body']['skuCode'] ) ){
            $body = ["resultCode"=>"0004","resultMsg"=>"skuCode非法"];
            $data = $cmicrwx->returnMsg($param,$body);
            return json($data);
        }

        $privateKey     = "cLa8mKQVebfkJS3y";
        $transactionId  = $param['contractRoot']['head']['transactionId'];
        $reqTime        = $param['contractRoot']['head']['reqTime'];
        $sign           = $param['contractRoot']['head']['sign'];
        $channelCode    = $param['contractRoot']['head']['channelCode'];
        $body           = json_encode($param['contractRoot']['body'],256);

        $req_sign = $transactionId . $reqTime . $privateKey . $body;
        $req_sign = md5($req_sign);

        if(strtolower($req_sign) != strtolower($sign)){
            logsToFile('zyjkerr.txt','入参参数:_'.json_encode($param,256));
            $body = ["resultCode"=>"0002","resultMsg"=>"验签失败"];
            $data = $cmicrwx->returnMsg($param,$body);
            return json($data);
        }

        $skucode = $param['contractRoot']['body']['skuCode'];
        $orderItemId = $param['contractRoot']['body']['orderItemId'];
        $reserved1 = isset($param['contractRoot']['body']['reserved1'])?$param['contractRoot']['body']['reserved1']:'';
       
        $location = $this->locationMerge($phone);
        $location = [
            "operator" => '',
            "home_location" => '',
            "province" => '',
            "city"     => '',
        ];

        //获取skucode信息
        $redis = new \think\cache\driver\Redis();
        $cmicrwx->redis = $redis;
        $cmicrwx->setSkuinfo($skucode);
        $skuinfo = $cmicrwx->getSkuinfo();
        // $redis->close();
        if ($skucode == 'AFYFUg') {
            $param['time'] = date('Y-m-d H:i:s');
            $zyjk_list = json_encode($param);
            $redis->rPush("zyjk_list", $zyjk_list);
        }

        if( !$skuinfo ){
            $skuinfo = Db::name("act_list_source")->where("skucode",$skucode)->find();
            if (!$skuinfo) {
                $body = ["resultCode"=>"0004","resultMsg"=>"skuCode参数非法","recoId"=>""];
                $data = $cmicrwx->returnMsg($param,$body);
                return json($data);
            }
        }

        $GzSms = new GzSms();
        $r_id = [];
        $coupon_status = 0;
        $skutype = $skuinfo['skutype'];

        if( in_array($skucode,['BlBQVA-m','AFYFUg','A1VQVA']) || $skutype == 2 ){//猫眼月卡
            $maoyan = new \app\web\model\MaoYan();
            $mobileNo = $phone;
            $channelOrderId = $param['contractRoot']['body']['orderItemId'];//订单ID
            $payMoney = $param['contractRoot']['body']['price'];
            switch ($skucode) {
                case 'AFYFUg':
                    $smsCode = 'mao-C';
                    break;
                case 'A1VQVA':
                case 'BlBQVA-m':
                    $smsCode = 'mao-B';
                    break;
                default:
                    $smsCode = 'mao-B';
                    break;
            }
            $myParam = [
                'skucode' => $skucode,
                'mobile' => $mobileNo,
                'channelOrderId' => $channelOrderId,
                'location' => $location,
                'getType' => 0,
                'payMoney' => $payMoney,
                'smsCode' => $smsCode,
                'getUrl' => '',
                'reserved1' => $reserved1
            ];
            $body = $maoyan->record($GzSms,$myParam);
            $data = $cmicrwx->returnMsg($param,$body);
            return json($data);
        }

        if($two_res['status'] != 200 ){
            $body = ["resultCode"=>"2","resultMsg"=>"失败","recoId"=>""];
            $data = $cmicrwx->returnMsg($param,$body);
            return json($data);
        }else{
            $body = ["resultCode"=>"1","resultMsg"=>"成功","recoId"=>$orderItemId];
            $data = $cmicrwx->returnMsg($param,$body);
            return json($data);
        }
    }

} 
